package com.example.cnedujnux2020103346;

public class BowlingGame {
    int[] pins =new int[21];
    int currentPinIndex=0;
    public void roll(int pin) {
        pins[currentPinIndex++]=pin;
    }

    public int score() {
        int totalScore=0;
        int currentFrameScoreIndex=0;
        for(int currentFrame=0;currentFrame<10;currentFrame++){ //currentFrame轮次，最多10轮
            totalScore += pins[currentFrameScoreIndex];
            totalScore += pins[currentFrameScoreIndex + 1];
            if (isAStrike(currentFrameScoreIndex)) {
                totalScore += pins[currentFrameScoreIndex + 2];
            }
            else if(isASpare(currentFrameScoreIndex)){
                totalScore += pins[currentFrameScoreIndex + 2];
                currentFrameScoreIndex++;
            }
            else {
                currentFrameScoreIndex++;
            }
            currentFrameScoreIndex++;
        }
        return totalScore;
    }

    private boolean isASpare(int scoreindex) { //如果是一个补中，就把后一个球的分数也计入
        return 10 == pins[scoreindex] + pins[scoreindex + 1];
    }
    private boolean isAStrike(int scoreindex) { //如果是一个补中，就把后一个球的分数也计入
        return 10 == pins[scoreindex];
    }
}
